/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: zhangti
 * @Date: 2019-10-25 11:25:10
 * @LastEditors: sueRimn
 * @LastEditTime: 2019-11-20 15:54:22
 */
/**
 * 可视域分析 
 * beyon
 */
import Analyser from '../analyser.js';
export default class VisualField extends Analyser{
	/**
	 * 初始化
	 * @param {*} supers 
	 * @param {*} opt 
	 */
	constructor(supers,opt){
		super(supers);
		//opt = Cesium.defaultValue(opt, Cesium.defaultValue.EMPTY_OBJECT);	//判断是否有值
		this.opt = opt;
		this.analyser();
	}
	/**
	 * 创建分析
	 */
	analyser(){
        if (!Cesium.defined(this._viewer)) {
            throw new Cesium.DeveloperError('viewer is required.');
		}
		return new VisualFieldEffect(this);
	}
}
/**
 * 内部分析类
 * @param {*} _that 
 */
const VisualFieldEffect = class {
	constructor(_that){
		this._that = _that;
		this.options = _that.opt;
		this.viewer = _that._viewer;
		this.id = Cesium.createGuid();
		this._tip= _that.entitys.add({
			id:this.id,
			label : {
				name: 'visualFieldEffect',
				show : false,
				showBackground : true,
				font : '14px monospace',
				horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
				verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
				pixelOffset : new Cesium.Cartesian2(15, -10)
			}
		});
		this._shadowPrimitive=undefined;
		this.state = _that.BEYONANALYSER_STATE.PREPARE;
		this.init();
	}
	init(){
		//移动
		var _self = this,_that = _self._that,viewer = _self.viewer,info = "";
		_that.handler.setInputAction(function (movement) {
			//var cartesian = viewer.scene.pickPosition(movement.endPosition);  
			let cartesian = _that.mouseManager.piTerrainToModule(movement.endPosition);
			if(_self.state===_that.BEYONANALYSER_STATE.END){               
				info ='点击设定起点';
				_that.showTip(_self._tip,true,cartesian,info);
			}else if(_self.state===_that.BEYONANALYSER_STATE.OPERATING){
				_self._shadowPrimitive.setPoseByTargetPoint(cartesian);   
				info ='点击分析可视域';
				_that.showTip(_self._tip,true,cartesian,info);         
			}
		},Cesium.ScreenSpaceEventType.MOUSE_MOVE);
		_that.handler.setInputAction(function (movement) {
			let pos ;
			pos = _that.mouseManager.piTerrainToModule(movement.position);
			if(!pos){
				pos = _that.mouseManager.screenToWorld(movement.position);
				if(!pos)return false;
			}
			if(!Cesium.defined(_self._shadowPrimitive)){
				_self._shadowPrimitive = new Cesium.ShadowPrimitive({
					scene:viewer.scene,
					viewerPosition:pos
				});
				viewer.scene.primitives.add(_self._shadowPrimitive);
				_self.state=_that.BEYONANALYSER_STATE.OPERATING;
			}else{
				_self.state=_that.BEYONANALYSER_STATE.END;
				_that.showTip(_self._tip,false);    
			}
		},Cesium.ScreenSpaceEventType.LEFT_CLICK);
	}
	remove(){
		//viewer.scene.primitives.add(this._shadowPrimitive);
		this._shadowPrimitive.show=false;
		this.viewer.entities.remove(this._tip);   
		this._tip=undefined;   
	}
	
}